Skip to content

Wait for initialisation instead of a fixed sleep in byte-communication tests - #321

Merged
JongChern merged 1 commit into
masterfrom
fix_flaky_connect_test
Oct 12, 2026
Merged

JongChern merged 1 commit into
masterfrom
fix_flaky_connect_test

Conversation

@MAzalya

@MAzalya MAzalya commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Problem

The byte-communication simulator tests in ShimmerDriverPC called connect() and then slept a fixed 3–5 s before asserting on the device. isConnected() turns true before the post-connect config read (infomem, calibration) runs, so on a loaded CI runner that read can miss the window.

Seen in Shimmer-Advance-API run 37567210311: API_0000X_ByteCommunicationShimmer3lns0_16_11_w_sd_bt_streaming.test001_testConnectandDisconnect failed at getTrialDurationEstimatedInSecs() != 1. The log shows the connect stalling after Connecting START with no infomem read inside the 5 s. A rerun passed.

Change (test code only)

  • New ByteCommunicationTestUtils.connectAndWaitUntilInitialised(): calls connect(), then waits until isInitialised() and the CONNECTED state. It has a 30 s ceiling and fails with the current state on timeout.
  • All 22 connect waits across 14 test classes now use it. The TaskCompletionSources they replace were never completed, so they were only ever fixed sleeps.
  • @After now disconnects the device, so earlier tests' IO threads and status timers no longer keep running into later tests (visible in the failing log as other ShimmerPC instances still polling GET_STATUS).
  • The 2 s mStreamingTask waits in the streaming tests are unchanged. They wait for data packets, which is a separate concern.

The w_sd_only, w_sync_master and w_sync_slave classes were also updated but are still excluded in ShimmerDriverPC/build.gradle; that exclusion is unchanged.

Testing

ShimmerDriverPC> gradlew :test --rerun: 54/54 pass locally (JDK 11). The previously failing test now completes in ~1 s rather than always sleeping 5 s. It ran once locally, so CI on this PR is the real check under load.

🤖 Generated with Claude Code

…n tests

The simulator connect tests called connect() and then slept a fixed 3-5 s
before asserting on the device. isConnected() turns true before the
post-connect config read (infomem, calibration) runs, so on a loaded CI
runner the read could miss the window and assertions such as
getTrialDurationEstimatedInSecs() failed - seen in Shimmer-Advance-API
run 37567210311, which passed on rerun.

Add ByteCommunicationTestUtils.connectAndWaitUntilInitialised(), which
waits until isInitialised() and the CONNECTED state (30 s ceiling, fails
with a message on timeout), and use it for all 22 connect waits. The
TaskCompletionSources it replaces were never completed, so they were only
ever sleeps. Also disconnect the device in @after so earlier tests' IO
threads and status timers no longer run on into later tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@JongChern JongChern left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. This is test-only and fixes the flaky connect test.

  • Test-only. All 15 files are under ShimmerDriverPC/src/test. There are no driver changes, and the test exclusions in build.gradle are unchanged.
  • It fixes the failure we kept seeing. That assertion is getTrialDurationEstimatedInSecs() != 1, which comes from the config bytes. initializeShimmer3or3R queues readConfigBytes() before readStatusLogAndStream(), and the helper can't return until the status reply has moved the state to CONNECTED. So the config is always parsed by the time the asserts run, however slow the runner.
  • The removed TaskCompletionSource waits were only fixed sleeps. Nothing ever completed them, so replacing them changes no behaviour.
  • The @After disconnect is safe. Each test creates a fresh ShimmerPC in @Before, so disconnecting afterwards can't affect later tests, and it stops earlier tests' threads polling the simulator. The 30 s limit reports the state when it fails.
  • Ran locally three times: all 11 active classes ran, 19/19 passed each time, and no case took over 5 s.

Suggestion (non-blocking)

"isInitialised() and CONNECTED" becomes true partway through the connect sequence:

  • When the status reply arrives, ShimmerPC.eventLogAndStreamStatusChanged moves the state from CONNECTING to CONNECTED, and ShimmerDeviceCallbackAdapter.setBluetoothRadioState sets isInitialised(true) alongside it.
  • That happens before the battery, Bluetooth firmware version, calibration dump and inquiry steps.
  • The true end of the sequence is inquiryDone(), which calls isReadyForStreaming() and sends NOTIFICATION_SHIMMER_FULLY_INITIALIZED.

The config-based asserts are covered either way. A test that checks calibration, such as Shimmer3R test003_ConnectandTestCalibParamRead, could still race the calibration-dump read on a loaded runner. These classes already receive callbacks through BasicProcessWithCallBack, so waiting for that notification would close the gap. As the description notes, the streaming tests' 2 s data waits are also still fixed sleeps.

@JongChern
JongChern merged commit f76a208 into master Oct 12, 2026
2 checks passed
@JongChern
JongChern deleted the fix_flaky_connect_test branch October 12, 2026 06:17
JongChern added a commit that referenced this pull request Oct 15, 2026
The instruction list is guarded by two locks, the device's monitor and
the list's own. Every path takes them device first, list second, except
getInstruction() on the IOThread, which held the list's lock and then
called the synchronized getListofInstructions(). initializeShimmer3or3R()
starts the read-status timer with no delay just as the IOThread picks up
the first post-connect instruction, and the timer's
getListofInstructions() takes the two the other way round. When they met,
both blocked for good: the connect never finished, and disconnect()
could not stop the IOThread ("did not terminate within join timeout").

Thread dumps from Linux CI (lns0_16_11_w_sd_bt_streaming) and from a
local Windows run (API_0000X_ByteCommunicationShimmer3) show the same
cycle. #321 did not cause it. It made connects follow one another
without the old fixed sleeps, which made the race land on CI every time.

getInstruction() now reads the list directly under the list's lock only.
API_00029 forces the interleaving: a stand-in timer holds the device while
the IOThread takes an instruction, then asks for the list. It fails on
the old code and passes on the new.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants